package com.junjiao.util.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
/**
 * 防止重复提交
 * @description:
 * @author:jiaojun [junjiao.j@gmail.com]
 * @project：exchange[置换]
 * @version:v1.0
 * @date:2013-3-23
 */
public class HttpUtil {
    
    private static String errorMessage = "";

    /**
     * 设置版本号 防止重复递交,重复删除
     * 
     * @param request
     */
    public static void setSubID(HttpServletRequest request,String prefix) {
	String subID = RandomStringUtils.random(11, true, true);
	request.setAttribute("subID_"+prefix, subID);
	request.getSession().setAttribute("subID_"+prefix, subID);
    }

    /**
     * 检查版本号 防止重复提交
     * 
     * @param request
     * @return
     */
    public static boolean checkSubID(HttpServletRequest request,String prefix) {
	Object subOrig = request.getSession().getAttribute("subID_"+prefix);
	String subID = request.getParameter("subID_"+prefix);
	if (subOrig == null || !subOrig.toString().equalsIgnoreCase(subID)) {
	    errorMessage = "请不要重复提交";
	    return false;
	}
	setSubID(request,prefix);
	return true;
    }

    public String getErrorMessage() {
	return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
	this.errorMessage = errorMessage;
    }

}
